﻿/*********************************************************************
 *
 *                             NOTICE
 *               COPYRIGHT (c) 2012 Panoscopix Software, Inc.
 *                UNPUBLISHED - ALL RIGHTS RESERVED
 * 
 *      This program is confidential, proprietary, and a trade
 *   secret of Panoscopix Software, Inc. The receipt or possession of
 *     this program does not convey any rights to reproduce or
 *      disclose its contents, or to manufacture, use, or sell
 *       anything that it may describe, in whole or in part, 
 *   without the specific written consent of Panoscopix Software, Inc.
 *
 * Resources (singleton managers, utilities, etc) for the exercise.
 * 
 *******************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using FileSystem;
using FileSystem.Test;
using FileSystem.Test.TestCases;

namespace FileSystem.Impl
{
    class AppResources
    {
	    private static AppResources instance;
	    private ICommandStackManager manager;
	    private IInputGenerator input;
        private IInputProcessor processor;
        private IOutputSink output;
        private CommandPrototypeFactory factory;
        private ActionPrototypeFactory actionfactory;
	    private FSFileSystem fileSystem;
	
        private AppResources() {
            initInteractive();
            //initTest();
        }

	    private void initInteractive() {
		    manager = new CommandStackManager();
		    input = new CliInputGenerator();
            processor = new InputProcessor();
            output = new ConsoleOutputSink();
		    factory = null;
		    fileSystem = null;
	    }

        private void initTest() {
            manager = new CommandStackManager();
            TestCoordinator tc = new TestCoordinator(new TestAll());
            input = tc;
            processor = new InputProcessor();
            output = tc;  //For evaluation.
            factory = null;
            fileSystem = null;
        }

        public static AppResources getInstance() {
		    if (null == instance) {
			    instance = new AppResources();
		    }
		    return instance;
	    }
	
	    public ICommandStackManager getManager() {
		    return manager;
	    }
	
	    public IInputGenerator getInput() {
		    return input;
	    }

        public IInputProcessor getProcessor() {
            return processor;
        }

        public IOutputSink getOutput() {
            return output;
        }

        public ICommandFactory getCommandFactory() {
		    if (null == factory) {
			    factory = CommandPrototypeFactory.getInstance();
		    }
		    return factory;
	    }

        public IActionFactory getActionFactory()
        {
            if (null == actionfactory)
            {
                actionfactory = ActionPrototypeFactory.getInstance();
            }
            return actionfactory;
        }

	    public ICommandPrototypeRegistry getCommandRegistry() {
		    if (null == factory) {
			    factory = CommandPrototypeFactory.getInstance();
		    }
		    return factory;
	    }

     
	    public FSFileSystem getFileSystem() {
		    if (null == fileSystem) {
			    fileSystem = FSFileSystem.getInstance();
		    }
		    return fileSystem;
	    }

        //NOT needed until Day 2
	    public void delay() {
		    delay(10);
	    }
	
	    public void delay(int seconds) {
		    try {
			    Thread.Sleep(seconds * 1000);
		    } catch (Exception ) {
			    //ignore.
		    }		
	    }
    }
}
